home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / kernel / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-14  |  34.6 KB  |  1,255 lines  |  [TEXT/KAHL]

  1. /* General Xconq initialization.
  2.    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
  3.    Stanley T. Shebs.
  4.  
  5. Xconq is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.  See the file COPYING.  */
  9.  
  10. /* Initialization is complicated, because xconq needs lots of setup for
  11.    maps, units, sides, and the like.  The data must also be able to come
  12.    from saved games, scenarios, bare maps in files, type definitions,
  13.    or be synthesized if necessary. */
  14.  
  15. /* The general theory is that everything gets set to a known empty state,
  16.    then all files are read, then all the synthesis methods get run.
  17.    Files/readers and synth methods are each responsible for preventing
  18.    fatal conflicts. */
  19.  
  20. #include "conq.h"
  21.  
  22. static void init_view_2 PROTO ((Side *side, int terrainset));
  23. static void init_view_cell PROTO ((int x, int y));
  24. static void maybe_init_view_cell PROTO ((int x, int y));
  25. static int adj_seen_terrain PROTO ((int x, int y, Side *side));
  26.  
  27. /* This is true when the set of types has been defined.  Subsequently only
  28.    scenarios based on those types can be loaded. */
  29.  
  30. int typesdefined = FALSE;
  31.  
  32. /* This is true after the game is totally synthesized, but players may
  33.    not all be in the game yet. */
  34.  
  35. int gamedefined = FALSE;
  36.  
  37. /* These are handy tmp vars, usually used with function args. */
  38.  
  39. short tmputype;
  40. short tmpmtype;
  41. short tmpttype;
  42.  
  43. /* The array of player/side matchups. */
  44.  
  45. Assign *assignments = NULL;
  46.  
  47. /* The pathname to the library directory. */
  48.  
  49. char *xconqlib;
  50.  
  51. /* The name of the default game module. */
  52.  
  53. char *standard_game_name;
  54.  
  55. /* This is the main printing buffer. */
  56.  
  57. char spbuf[BUFSIZE];
  58.  
  59. /* This is an auxiliary printing buffer. */
  60.  
  61. char tmpbuf[BUFSIZE];
  62.  
  63. #ifdef DESIGNERS
  64.  
  65. /* This is true if all sides with displays should become designers
  66.    automatically. */
  67.  
  68. int allbedesigners = FALSE;
  69.  
  70. /* This is the current count of how many sides are designers. */
  71.  
  72. int numdesigners = 0;
  73.  
  74. #endif /* DESIGNERS */
  75.  
  76. int max_zoc_range;
  77.  
  78. /* The table of all available synthesis methods. */
  79.  
  80. struct synthtype {
  81.     int key;
  82.     int (*fn) PROTO ((void));
  83.     int dflt;
  84. } synthmethods[] = {
  85.     { K_MAKE_FRACTAL_PTILE_TERRAIN, make_fractal_terrain, TRUE },
  86.     { K_MAKE_RANDOM_TERRAIN, make_random_terrain, FALSE },
  87.     { K_MAKE_EARTHLIKE_TERRAIN, make_earthlike_terrain, FALSE },
  88.     { K_MAKE_MAZE_TERRAIN, make_maze_terrain, FALSE },
  89.     { K_MAKE_RIVERS, make_rivers, TRUE },
  90.     { K_MAKE_COUNTRIES, make_countries, TRUE },
  91.     { K_MAKE_INDEPENDENT_UNITS, make_independent_units, TRUE },
  92.     { K_MAKE_INITIAL_MATERIALS, make_initial_materials, TRUE },
  93.     { K_NAME_UNITS_RANDOMLY, name_units_randomly, TRUE },
  94.     { K_NAME_GEOGRAPHICAL_FEATURES, name_geographical_features, TRUE },
  95.     { K_MAKE_ROADS, make_roads, TRUE },
  96.     { K_MAKE_WEATHER, make_weather, TRUE },
  97.     { K_MAKE_RANDOM_DATE, make_random_date, FALSE },
  98.     { -1, NULL, 0 }
  99. };
  100.  
  101. /* Allow environment vars to override compiled-in library and game. */
  102.  
  103. void
  104. init_library_path(path)
  105. char *path;
  106. {
  107.     xconqlib = getenv("XCONQLIB");
  108.     if (empty_string(xconqlib)) {
  109.     if (!empty_string(path)) {
  110.         xconqlib = path;
  111.     } else {
  112.         xconqlib = default_library_filename();
  113.     }
  114.     }
  115.     standard_game_name = getenv("XCONQSTANDARDGAME");
  116.     if (empty_string(standard_game_name))
  117.       standard_game_name = STANDARD_GAME;
  118. }
  119.  
  120. /* This is a general init that prepares data structures to be filled in
  121.    by a game definition.  It should run *before* command line parsing. */
  122.  
  123. void
  124. init_data_structures()
  125. {
  126. #ifdef DEBUGGING
  127.     /* Map all the debugging outputs to stdout. */
  128.     /* (I wonder why I didn't think stderr was a better choice...) */
  129. #ifdef USE_CONSOLE
  130.        dfp  = stdout;
  131.     dgfp = stdout;
  132.     dmfp = stdout;
  133. #else
  134.     /* should open files or something */
  135. #endif /* USE_CONSOLE */
  136. #endif /* DEBUGGING */
  137.  
  138.     init_xrandom(-1);
  139.     init_lisp();
  140.     init_types();
  141.     init_globals();
  142.     init_namers();
  143.     init_world();
  144.     init_history();
  145.     init_sides();
  146.     init_agreements();
  147.     init_units();
  148.     init_help();
  149.     init_scorekeepers();
  150.     init_actions();
  151.     init_tasks();
  152. }
  153.  
  154. /* Build the default list of synthesis methods. */
  155.  
  156. void
  157. set_g_synth_methods_default()
  158. {
  159.     int i;
  160.     Obj *synthlist = lispnil, *synthlistend = lispnil, *tmp;
  161.  
  162.     for (i = 0; synthmethods[i].key >= 0; ++i) {
  163.     if (synthmethods[i].dflt) {
  164.         tmp = cons(intern_symbol(keyword_name(synthmethods[i].key)),
  165.                lispnil);
  166.         if (synthlist == lispnil) {
  167.         synthlist = synthlistend = tmp;
  168.         } else {
  169.         synthlistend->v.cons.cdr = tmp;
  170.         synthlistend = tmp;
  171.         }
  172.     }
  173.     }
  174.     /* Now alter the global variable to contain this list. */
  175.     set_g_synth_methods(synthlist);
  176. }
  177.  
  178. /* Run a doublecheck on plausibility of game parameters.  Additional
  179.    checks are performed elsewhere as needed, for instance during random
  180.    generation.  Serious mistakes exit now, since they can cause all sorts
  181.    of strange behavior and core dumps.  It's a little more friendly to only
  182.    exit at the end of the tests, so all the mistakes can be found at once. */
  183.  
  184. /* In theory, if a game passes these tests, then Xconq will never crash. */
  185.  
  186. /* (This should always be run, might find errors in compiled game.) */
  187.  
  188. void
  189. check_game_validity()
  190. {
  191.     int failed = FALSE, movers = FALSE, actors = FALSE;
  192.     int u1, u2, m1, t1, t2;
  193.  
  194.     /* We must have at least one kind of unit. */
  195.     if (numutypes < 1) {
  196.     init_error("no unit types have been defined");
  197.     failed = TRUE;
  198.     }
  199.     /* OK not to have any types of materials. */
  200.     /* We must have at least one kind of terrain. */
  201.     if (numttypes < 1) {
  202.     init_error("no terrain types have been defined");
  203.     failed = TRUE;
  204.     }
  205.     /* Make sure inter-country distances relate correctly. */
  206.     if (g_min_separation() >= 0
  207.     && g_max_separation() >= 0
  208.     && !(g_min_separation() <= g_max_separation())) {
  209.     init_warning("country separations %d to %d screwed up",
  210.              g_min_separation(), g_max_separation());
  211.     }
  212.  
  213.     /* (Need more general game checks.) */
  214.  
  215.     max_zoc_range = -1;
  216.     /* Check that all the unit names and chars are distinct. */
  217.     for_all_unit_types(u1) {
  218.     for_all_unit_types(u2) {
  219.         /* Only do "upper triangle" of utype x utype matrix. */
  220.         if (u1 < u2) {
  221.         if (strcmp(u_type_name(u1), u_type_name(u2)) == 0) {
  222.             init_warning(
  223.              "unit types %d and %d are both named \"%s\"",
  224.                  u1, u2, u_type_name(u1));
  225.             /* This is bad but not disastrous, so don't fail. */
  226.         }
  227.         }
  228.         if (uu_zoc_range(u1, u2) > max_zoc_range) {
  229.         max_zoc_range = uu_zoc_range(u1, u2);
  230.         }
  231.     }
  232.     }
  233.     /* Large ZOC ranges are not implemented. */
  234.     if (max_zoc_range > 1) {
  235.     init_warning("ZOC range goes up to %d, may be very inefficient", max_zoc_range);
  236.     }
  237.     /* (Eventually check material types also.) */
  238.     /* Check that all terrain names and chars are distinct. */
  239.     for_all_terrain_types(t1) {
  240.     for_all_terrain_types(t2) {
  241.         /* Only do "upper triangle" of ttype x ttype matrix. */
  242.         if (t1 < t2) {
  243.         if (strcmp(t_type_name(t1), t_type_name(t2)) == 0) {
  244.             init_warning(
  245.              "terrain types %d and %d are both named \"%s\"",
  246.              t1, t2, t_type_name(t1));
  247.             /* This is bad but not disastrous, so don't fail. */
  248.         }
  249.         /* Should check that colors are different from each other
  250.            and from builtin colors? */
  251.         }
  252.     }
  253.     }
  254.     /* Check various unit type properties. */
  255.     for_all_unit_types(u1) {
  256.     /* Can't make use of this yet, so error out if anybody tries. */
  257.     if (u_available(u1) != 1) {
  258.         init_error("unit type %d must always be available", u1);
  259.         failed = TRUE;
  260.     }
  261.     /* Can't make use of this yet, so error out if anybody tries. */
  262.     if (u_action_priority(u1) != 0) {
  263.         init_error("unit type %d cannot have a nonzero action priority", u1);
  264.         failed = TRUE;
  265.     }
  266.     /* should be part of general bounds check */
  267.     if (u_cp(u1) <= 0) {
  268.         init_error("unit type %d has nonpositive cp", u1);
  269.         failed = TRUE;
  270.     }
  271.     if (u_hp(u1) <= 0) {
  272.         init_error("unit type %d has nonpositive hp", u1);
  273.         failed = TRUE;
  274.     }
  275.     if (u_parts(u1) <= 0 || u_hp(u1) % u_parts(u1) != 0) {
  276.         init_error("unit type %d hp not a multiple of its parts.", u1);
  277.         failed = TRUE;
  278.     }
  279.     if (u_speed(u1) > 0) {
  280.         movers = TRUE;
  281.     }
  282.     if (u_acp(u1) > 0) {
  283.         actors = TRUE;
  284.     }
  285.     }
  286.     /* Check various material type properties. */
  287.     for_all_material_types(m1) {
  288.     /* Can't make use of this yet, so error out if anybody tries. */
  289.     if (m_available(m1) != 1) {
  290.         init_error("material type %d not always available", m1);
  291.         failed = TRUE;
  292.     }
  293.     }
  294.     /* Check various terrain type properties. */
  295.     for_all_terrain_types(t1) {
  296.     /* Can't make use of this yet, so error out if anybody tries. */
  297.     if (t_available(t1) != 1) {
  298.         init_error("terrain type %d not always available", t1);
  299.         failed = TRUE;
  300.     }
  301.     }
  302.     /* If nothing can move and nothing can build, this will probably be
  303.        a really dull game, but there may be such games, do don't say
  304.        anything normally. */
  305.     if (numutypes > 0 && !actors) {
  306.     Dprintf("No actors have been defined.\n");
  307.     }
  308.     if (numutypes > 0 && !movers) {
  309.     Dprintf("No movers have been defined.\n");
  310.     }
  311.     if (numttypes > 0 && numcelltypes == 0) {
  312.     init_error("no terrain type has been allowed for cells");
  313.     failed = TRUE;
  314.     }
  315.     /* This is a really bad game definition, leave before we crash.  This would
  316.        only be executed on systems where init_error doesn't exit immediately. */
  317.     if (failed) {
  318.     exit(0);
  319.     }
  320.     Dprintf("Finished checking game design.\n");
  321.     Dprintf("It defines %d unit types, %d material types, %d terrain types.\n",
  322.         numutypes, nummtypes, numttypes);
  323. }
  324.  
  325. /* Calculate the values of global variables that are used everywhere. */
  326.  
  327. void
  328. calculate_globals()
  329. {
  330.     /* The game is now completely defined; no further user-specified changes can
  331.        occur. */
  332.     gamedefined = TRUE;
  333.     calculate_world_globals();
  334.     /* This needs to be precalculated instead of as-needed, since range can be
  335.        validly both negative and positive, so no way to distinguish uninitialized. */
  336.     {
  337.     int u1, u2, range;
  338.         extern int max_detonate_on_approach_range;
  339.  
  340.     max_detonate_on_approach_range = -1;
  341.     for_all_unit_types(u1) {
  342.         for_all_unit_types(u2) {
  343.         range = uu_detonate_approach_range(u1, u2);
  344.         max_detonate_on_approach_range = max(range, max_detonate_on_approach_range);
  345.         }
  346.     }
  347.     }    
  348. }
  349.  
  350. /* Clean up all the objects and cross-references. */
  351.  
  352. void
  353. patch_object_references()
  354. {
  355.     int numhere, numoccs;
  356.     Unit *unit, *unit2, *transport;
  357.     Side *side;
  358.     Obj *utref, *utorig;
  359.  
  360.     /* Use read-in ids to fill in side slots that point to other objects. */
  361.     for_all_sides(side) {
  362.     if (side->playerid > 0) {
  363.         side->player = find_player(side->playerid);
  364.     }
  365.     if (side->controlledbyid > 0) {
  366.         side->controlledby = side_n(side->controlledbyid);
  367.     }
  368.     if (side->selfunitid > 0) {
  369.         side->selfunit = find_unit(side->selfunitid);
  370.     }
  371.     }
  372.     for_all_units(unit) {
  373.     /* It's possible that dead units got read in, so check. */
  374.     if (alive(unit)) {
  375.         if (unit->transport != NULL) {
  376.         transport = NULL;
  377.         utref = utorig = (Obj *) unit->transport;
  378.         /* For safety's sake, null out the slot. */
  379.         unit->transport = NULL;
  380.         /* We have a Lisp object; use it to identify a particular
  381.            unit as the transport. */
  382.         if (symbolp(utref) && boundp(utref)) {
  383.             utref = symbol_value(utref);
  384.         }
  385.         if (numberp(utref)) {
  386.             transport = find_unit(c_number(utref));
  387.             if (transport == NULL)
  388.               init_warning("could not find a transport id %d for %s",
  389.                    c_number(utref), unit_desig(unit));
  390.         } else if (stringp(utref)) {
  391.             transport = find_unit_by_name(c_string(utref));
  392.             if (transport == NULL)
  393.               init_warning("could not find a transport named \"%s\" for %s",
  394.                    c_string(utref), unit_desig(unit));
  395.         } else {
  396.             /* not a recognized way to refer to a unit */
  397.             sprintlisp(tmpbuf, utorig);
  398.             init_warning("could not find transport %s for %s",
  399.                  tmpbuf, unit_desig(unit));
  400.         }
  401.         /* it is important to make sure that unit->x, and unit->y 
  402.            are negative at this point.  Otherwise, the coverage will
  403.            be messed up for units put into transports that have not yet
  404.            been placed.  They will be covered for entering the cell,
  405.            and again when the transport enters the cell. */
  406.         if (transport != NULL) {
  407.             /* (also check that this is a valid transport type?) */
  408.             enter_transport(unit, transport);
  409.         } else {
  410.             /* (could let the unit enter the cell, or could
  411.                make it infinitely postponed) */
  412.         }
  413.         } else {
  414.         /* Check that the unit's location is meaningful. */
  415.         if (!inside_area(unit->x, unit->y)) {
  416.             if (inside_area(unit->prevx, unit->prevy)) {
  417.             if (can_occupy_cell(unit, unit->prevx, unit->prevy)) {
  418.                 enter_cell(unit, unit->prevx, unit->prevy);
  419.             } else {
  420.                 numhere = numoccs = 0;
  421.                 /* Search this cell for units to enter. */
  422.                 for_all_stack(unit->prevx, unit->prevy, transport) {
  423.                 ++numhere;
  424.                 if (unit->side == transport->side
  425.                     && can_occupy(unit, transport)) {
  426.                     enter_transport(unit, transport);
  427.                     break;
  428.                 }
  429.                 if (unit->side == transport->side
  430.                     && can_occupy(transport, unit))
  431.                   ++numoccs;
  432.                 }
  433. #if 0
  434.                 /* Try having all the existing units enter the transport. */
  435.                 /* (but doesn't work if only some units should go into transport) */
  436.                 if (!inside_area(unit->x, unit->y) && numoccs == numhere) {
  437.                 for_all_stack(unit->prevx, unit->prevy, unit2) {
  438.                     enter_transport(unit2, unit);
  439.                 }
  440.                 enter_cell(unit, unit->prevx, unit->prevy);
  441.                 }
  442. #endif
  443.                 if (!inside_area(unit->x, unit->y)) {
  444.                 init_warning("No room for %s at %d,%d",
  445.                          unit_desig(unit),
  446.                          unit->prevx, unit->prevy);
  447.                 }
  448.             }
  449.             /* This prevents attempts to do a second enter_cell
  450.                during initialization. */
  451.             unit->prevx = unit->prevy = -1;
  452.             } else if (unit->prevx == -1 && unit->prevy == -1) {
  453.                 /* This will be left alone - should have pos filled in later. */
  454.             } else if (unit->cp >= 0) {
  455.             /* Warn, but only if there's no good reason for the unit to have
  456.                an offworld position. */
  457.             if (area.fullwidth == 0) {
  458.                 init_warning("%s is at offworld location, left there",
  459.                      unit_desig(unit));
  460.             }
  461.             /* This will make it be a reinforcement. */
  462.             unit->cp = -1;
  463.             }
  464.         }
  465.         }
  466.         /* Make sure that side numbering will use only new numbers. */
  467.         if (unit->side != NULL) {
  468.         if (unit->number > 0) {
  469.             (unit->side->counts)[unit->type] =
  470.               max((unit->side->counts)[unit->type], 1 + unit->number);
  471.         }
  472.         } else {
  473.         /* Trash the numbers on indep units. */
  474.         unit->number = 0;
  475.         }
  476.         if (completed(unit)) {
  477.         init_unit_actorstate(unit);
  478.         /* Restore acp that wasn't written out because it was the most normal
  479.            value. */
  480.         if (unit->act && unit->act->acp < u_acp_min(unit->type))
  481.           unit->act->acp = u_acp(unit->type);
  482.         /* Restore initacp that wasn't written out because it was the normal
  483.            value. */
  484.         if (unit->act
  485.             && unit->act->acp > 0
  486.             && unit->act->initacp == 0)
  487.           unit->act->initacp = u_acp(unit->type);
  488.         /* Might already have a plan, leave alone if so. */
  489.         if (unit->plan == NULL) {
  490.             init_unit_plan(unit);
  491.         }
  492.         }
  493.     } else {
  494.         /* Dead units need to be disentangled from anything that might
  495.            have been done to them.  For instance, a module might include
  496.            a standard collection of units, but then follow up by removing
  497.            some of those units, and can do it by setting hp == 0.  We want
  498.            this to work consistently and reliably. */
  499.         /* Null this out, any possible unit reference is useless. */
  500.         unit->transport = NULL;
  501.         if (inside_area(unit->x, unit->y)) {
  502.         leave_cell(unit);
  503.         }
  504.     }
  505.     }
  506. }
  507.  
  508. /* Make up a proposed side/player assignment, creating sides and players
  509.    as necessary.  Lock down any assignments that should not be changed,
  510.    but leave everything to be changed as desired. */
  511.  
  512. /* (when does locking get done?) */
  513.  
  514. void
  515. make_trial_assignments()
  516. {
  517.     int i = 0;
  518.     Side *side;
  519.     Player *player;
  520.  
  521.     /* Fill in the side's predefined default and range of initial advantage. */
  522.     for_all_sides(side) {
  523.         init_side_advantage(side);
  524.     }
  525.     /* Ensure we have as many sides as will be required. */
  526.     while (numsides < g_sides_min()) {
  527.     make_up_a_side();
  528.     }
  529.     while (numsides < min(numplayers, g_sides_max())) {
  530.     make_up_a_side();
  531.     }
  532.     if (numsides < numplayers) {
  533.     /* We have too many players. */
  534.     init_warning("too many players (%d)", numplayers);
  535.     numplayers = numsides; /* probably bogus */
  536.     /* Ideally, would ask whether to continue */
  537.     }
  538.     /* Put in all the sides. */
  539.     for_all_sides(side) {
  540.     assignments[i++].side = side;
  541.     }
  542.     /* If no players have been created, make one that is human-run,
  543.        presumably by the person who started up this program. */
  544.     if (numplayers == 0) {
  545.     add_default_player();
  546.     }
  547.     /* Add in enough default players for all the sides. */
  548.     while (numplayers < numsides) {
  549.     player = add_player();
  550.     /* Default players are always AIs (at least for now). */
  551.     player->aitypename = "mplayer";
  552.     }
  553.     /* (make any prespecified assignments) */
  554.     /* Assign any remaining players. */
  555.     player = playerlist;
  556.     for (i = 0; i < numsides; ++i) {
  557.     if (assignments[i].player == NULL) {
  558.         for (player = playerlist ; player != NULL; player = player->next) {
  559.         if (player->side == NULL) {
  560.             assignments[i].player = player;
  561.             player->side = assignments[i].side;
  562.             (assignments[i].side)->player = player;
  563.             break;
  564.         } else {
  565.         }
  566.         }
  567.     }
  568.     /* Set the player's advantage to be the side's advantage, if defined. */
  569.     if (assignments[i].player != NULL
  570.         && assignments[i].player->advantage == 0) {
  571.         assignments[i].player->advantage = assignments[i].side->advantage;
  572.     }
  573.     Dprintf("Tentatively assigned %s to %s%s\n",
  574.         side_desig(assignments[i].side),
  575.         player_desig(assignments[i].player),
  576.         (assignments[i].locked ? " (locked)" : ""));
  577.     }
  578.     /* At this point, we have matching sides and players, ready to be
  579.        rearranged if desired. */
  580. }
  581.  
  582. /* Create a random side with default characteristics. */
  583.  
  584. void
  585. make_up_a_side()
  586. {
  587.     Side *side = create_side();
  588.  
  589.     if (side == NULL) {
  590.     run_error("could not create a side");
  591.     return;
  592.     }
  593.     make_up_side_name(side);
  594.     init_side_advantage(side);
  595.     /* A newly-created side starts out in the game, can drop out later. */
  596.     side->ingame = TRUE;
  597.     Dprintf("Made up a side %s\n", side_desig(side));
  598. }
  599.  
  600. /* If undefined, seed a side's advantage and allowable range from the
  601.    global values. */
  602.  
  603. void
  604. init_side_advantage(side)
  605. Side *side;
  606. {
  607.     /* Set up the default and range of initial advantages. */
  608.     if (side->advantage == 0)
  609.       side->advantage = g_advantage_default();
  610.     if (side->minadvantage == 0)
  611.       side->minadvantage = g_advantage_min();
  612.     if (side->maxadvantage == 0)
  613.       side->maxadvantage = g_advantage_max();
  614. }
  615.  
  616. /* Add a side and a player to go with it (used by interfaces). */
  617.  
  618. int
  619. add_side_and_player()
  620. {
  621.     int n;
  622.     Side *side;
  623.     Player *player;
  624.  
  625.     make_up_a_side();
  626.     side = side_n(numsides);
  627.     if (side == NULL)
  628.       return FALSE;
  629.     n = numsides - 1;
  630.     assignments[n].side = side;
  631.     player = add_player();
  632.     assignments[n].player = player;
  633.     player->side = assignments[n].side;
  634.     (assignments[n].side)->player = player;
  635.     /* Set the player's advantage to be the side's advantage, if not
  636.        already set. */
  637.     if (player->advantage == 0) {
  638.     player->advantage = side->advantage;
  639.     }
  640.     return TRUE;
  641. }
  642.  
  643. /* This can be used by interfaces to exchange players between one side and
  644.    another. */
  645.  
  646. int
  647. exchange_players(n, n2)
  648. int n, n2;
  649. {
  650.     int i;
  651.     Player *tmpplayer = assignments[n].player;
  652.  
  653.     if (n < 0)
  654.       n = 0;
  655.     if (n2 < 0) {
  656.     for (i = n + 1; i <= numsides + n; ++i) {
  657.         n2 = i % numsides;
  658.         if (assignments[n2].side && (assignments[n2].side)->ingame) break;
  659.     }
  660.     /* No sides to exchange with, return. */
  661.         if (i == numsides + n)
  662.       return -1;
  663.     }
  664.     assignments[n].player = assignments[n2].player;
  665.     assignments[n2].player = tmpplayer;
  666.     /* Doesn't seem like these should be needed, but they are. */
  667.     assignments[n].player->side = assignments[n].side;
  668.     assignments[n].side->player = assignments[n].player;
  669.     assignments[n2].player->side = assignments[n2].side;
  670.     assignments[n2].side->player = assignments[n2].player;
  671.     return n2;
  672. }
  673.  
  674. int
  675. remove_side_and_player()
  676. {
  677.     /* (how to do this?) */
  678.     /* (would need to renumber sides etc) */
  679.     return FALSE;
  680. }
  681.  
  682. /* Synthesis methods fill in whatever is not fixed by game modules or by
  683.    the player(s). */
  684.  
  685. void
  686. run_synth_methods()
  687. {
  688.     int i, methkey, found = FALSE;
  689.     Obj *synthlist, *methods, *method, *parms;
  690.  
  691.     synthlist = g_synth_methods();
  692.     Dprintf("Will run syntheses ");  Dprintlisp(synthlist);  Dprintf("\n");
  693.     for (methods = synthlist; methods != lispnil; methods = cdr(methods)) {
  694.     method = car(methods);
  695.     if (symbolp(method)) {
  696.         methkey = keyword_code(c_string(method));
  697.         for (i = 0; synthmethods[i].key >= 0; ++i) {
  698.         if (methkey == synthmethods[i].key) {
  699.             (*synthmethods[i].fn)();
  700.             found = TRUE;
  701.             break;
  702.         }
  703.         }
  704.     } else if (consp(method)) {
  705.         parms = cdr(method);
  706.         if (stringp(car(method))) {
  707.         /* External program. */
  708.         /* (should format parms, compose outputfile redirection,
  709.             call program with os.c function, check return code,
  710.             open and read output file) */
  711.         run_error("No external synth programs yet");
  712.         }
  713.     }
  714.     if (!found) {
  715.         sprintlisp(spbuf, method);
  716.         init_warning("bad synthesis method %s, ignoring", spbuf);
  717.     }
  718.     }
  719. }
  720.  
  721. /* (should reindent) */
  722. int
  723. make_weather()
  724. {
  725.     int x, y, winddir;
  726.     extern int maxwindforce;    
  727.  
  728.     if (maxwindforce > 0) {
  729.     if (!winds_defined()) {
  730.         allocate_area_winds();
  731.         if (g_wind_mix_range() > 0) {
  732.         /* Make all winds start in the same direction. */
  733.         /* Initial variation will randomize. */
  734.         winddir = random_dir();
  735.         for_all_cells(x, y) {
  736.             set_wind_at(x, y, winddir,
  737.                 t_wind_force_avg(terrain_at(x, y)));
  738.         }
  739.         } else {
  740.         for_all_cells(x, y) {
  741.             set_wind_at(x, y, random_dir(),
  742.                 t_wind_force_avg(terrain_at(x, y)));
  743.         }
  744.         }
  745.     }
  746.     }
  747.     return TRUE;
  748. }
  749.  
  750. /* Set the starting date/time to a random value. */
  751.  
  752. int
  753. make_random_date()
  754. {
  755.     extern int baseyear;
  756.  
  757.     /* (this should tweak a "date offset" rather than curturn,
  758.        which should be actual turn number) */
  759.     baseyear = 1900 + xrandom(10);
  760.     return TRUE;
  761. }
  762.  
  763. /* The final init cleans up various stuff. */
  764.  
  765. void
  766. final_init()
  767. {
  768.     Side *side;
  769.     Unit *unit;
  770.  
  771.     /* Make sure each side has a self-unit if it needs one. */
  772.     for_all_sides(side) {
  773.     if ((g_self_required() /* || side prop? */)
  774.         && side->selfunit == NULL) {
  775.         for_all_side_units(side, unit) {
  776.         if (u_can_be_self(unit->type)
  777.             && in_play(unit)
  778.             && completed(unit))
  779.           side->selfunit = unit;
  780.         }
  781.     }
  782.     }
  783.     /* At this point we should be ready to roll.  Any inconsistencies
  784.        hereafter will be fatal. */
  785.     check_consistency();
  786.     /* If any sides' balance sheets were not restored, count the units now. */
  787.     init_side_balance();
  788.     /* Fix up garbled view data. */
  789.     init_all_views();
  790.     configure_sides();
  791.     /* Check again, just to be sure. */
  792.     check_consistency();
  793.     create_game_help_nodes();
  794.     /* Set up the scores to be attached to each side. */
  795.     init_scores();
  796.     /* Calculate which random event methods will be run. */
  797.     init_random_events();
  798.     /* Calculate extent and position of geographical features. */
  799.     compute_all_feature_centroids();
  800.     /* Report on memory consumption. */
  801.     Dprintf("One side is %d bytes.\n", sizeof(Side));
  802.     Dprintf("One unit is %d bytes, one plan is %d bytes.\n",
  803.         sizeof(Unit), sizeof(Plan));
  804.     if (Debug) report_malloc();
  805. }
  806.  
  807. /* Load up any player-specified configuration data. */
  808.  
  809. void
  810. configure_sides()
  811. {
  812.     Side *side;
  813.  
  814.     for_all_sides(side) {
  815.     load_side_config(side);
  816.     }
  817. }
  818.  
  819. /* Calculate what each side knows about the world. */
  820.  
  821. void
  822. init_all_views()
  823. {
  824.     int x, y, i = 0, todo = max(1, numsides * area_cells());
  825.     int terrainset;
  826.     Side *side;
  827.  
  828.     /* No view init needed if everything always visible. */
  829.     if (g_see_all())
  830.       return;
  831.     /* Set up the basic view structures for all sides first. */
  832.     for_all_sides(side) {
  833.     if (!g_see_all()) {
  834.         terrainset = init_view(side);
  835.         init_view_2(side, terrainset);
  836.     }
  837.     /* Can't think of any other place to put this... */
  838.     calc_start_xy(side);
  839.     }
  840.     announce_lengthy_process("Computing current view at each location");
  841.     /* (coverage is also done at this point) */
  842.     for_all_sides(side) {
  843.     for_all_cells(x, y) {
  844.         ++i;
  845.             if (i % 100 == 0) announce_progress((100 * i) / todo);
  846.         see_cell(side, x, y);
  847.     }
  848.     }
  849.     finish_lengthy_process();
  850.     init_area_views();
  851. }
  852.  
  853. static void
  854. init_view_2(side, terrainset)
  855. Side *side;
  856. int terrainset;
  857. {
  858.     int x, y;
  859.  
  860.     calc_coverage(side);
  861.     tmpside = side;
  862.     if (g_terrain_seen()) {
  863.     for_all_cells(x, y) {
  864.         init_view_cell(x, y);
  865.     }
  866.     } else {
  867.     for_all_cells(x, y) {
  868.         if (!terrainset)
  869.           set_terrain_view(side, x, y, UNSEEN);
  870.         set_unit_view(side, x, y, EMPTY);
  871.         /* View date is 0, which is what we want. */
  872.     }
  873.     }
  874. }
  875.  
  876. /* This is a helper for the following routine.  The helper is applied to
  877.    each cell, decides what is visible in that cell. */
  878.  
  879. /* This only works from already_seen, does not account for coverage. */
  880.  
  881. /* (should only be run once/cell/side, use a scratch layer to keep track?) */
  882.  
  883. static void
  884. init_view_cell(x, y)
  885. int x, y;
  886. {
  887.     int u, chance;
  888.     Unit *unit;
  889.  
  890.     /* Guaranteed to see the terrain accurately. */
  891.     set_terrain_view(tmpside, x, y, buildtview(terrain_at(x, y)));
  892.     /* If this cell is under observation, don't need to do anything special. */
  893.     if (tmpside->coverage && cover(tmpside, x, y) > 0)
  894.       return;
  895.     /* Scan all the units here to see if any are visible. */
  896.     for_all_stack(x, y, unit) {
  897.         if (in_play(unit)) {
  898.             u = unit->type;
  899.         if (u_see_always(u)) {
  900.             see_exact(tmpside, x, y);
  901.             /* or flag unit as spotted? */
  902.         if (tmpside->coverage)
  903.           set_cover(tmpside, x, y, 1);
  904.             return;
  905.         }
  906.         chance = (indep(unit) ? u_already_seen_indep(u)
  907.               : u_already_seen(u));
  908.         if (probability(chance)) {
  909.             see_exact(tmpside, x, y);
  910.             /* This view might be overwritten by a view of
  911.                another might-be-seen unit at this location,
  912.                so don't return just yet. */
  913.         }
  914.     }
  915.     }
  916. }
  917.  
  918. static void
  919. maybe_init_view_cell(x, y)
  920. int x, y;
  921. {
  922.     int dir, x1, y1;
  923.  
  924.     if (adj_seen_terrain(x, y, tmpside) && flip_coin()) {
  925.     init_view_cell(x, y);
  926.     for_all_directions(dir) {
  927.         if (point_in_dir(x, y, dir, &x1, &y1)) {
  928.         init_view_cell(x1, y1);
  929.         }
  930.     }
  931.     }
  932. }
  933.  
  934. static int
  935. adj_seen_terrain(x, y, side)
  936. int x, y;
  937. Side *side;
  938. {
  939.     int dir, x1, y1;
  940.  
  941.     if (!inside_area(x, y) || side == NULL)
  942.       return FALSE;
  943.     for_all_directions(dir) {
  944.     if (point_in_dir(x, y, dir, &x1, &y1)) {
  945.         if (terrain_view(side, x1, y1) != UNSEEN)
  946.           return TRUE;
  947.     }
  948.     }
  949.     return FALSE;
  950. }
  951.  
  952. /* Do ranged initial views from units. */
  953.  
  954. void
  955. init_area_views()
  956. {
  957.     int rad, x, y, pop, dir, x1, y1, i = 0;
  958.     Unit *unit;
  959.     Side *side, *side2;
  960.  
  961.     /* Don't run if nothing exists to look at. */
  962.     if (!terrain_defined())
  963.       return;
  964.     /* Skip if everything already known, side creation got these cases. */
  965.     if (g_see_all() || g_terrain_seen())
  966.       return;
  967.     announce_lengthy_process("Computing ranged and people views");
  968.     /* Compute the view for each side. */ 
  969.     for_all_sides(side) {
  970.     /* Set this so the helper fn has a side to use. */
  971.     tmpside = side;
  972.     /* View from our own and other units. */
  973.     for_all_units(unit) {
  974.         if (trusted_side(unit->side, side)) {
  975.         /* The unit always sees itself. */
  976.         see_exact(side, unit->x, unit->y);
  977.         /* It may also see things nearby. */
  978.         rad = u_seen_radius(unit->type);
  979.         if (rad >= area.maxdim) {
  980.             /* Special optimization - view the whole area. */
  981.             for_all_cells(x, y) {
  982.             init_view_cell(x, y);
  983.             }
  984.             /* Note that we're not done; other units may be able to
  985.                supply more exact views of their vicinities than
  986.                would init_view_cell from a distant unit. */
  987.         } else if (rad >= 0) {
  988.             apply_to_area(unit->x, unit->y, rad, init_view_cell);
  989.         }
  990.         }
  991.     }
  992.     /* The people see everything in the cells that they are in, plus the
  993.        normally visible things in adjacent cells. */
  994.     if (people_sides_defined()) {
  995.         for_all_interior_cells(x, y) {
  996.         pop = people_side_at(x, y);
  997.         side2 = side_n(pop);
  998.         if (pop != NOBODY && trusted_side(side2, side)) {
  999.             see_exact(side, x, y);
  1000.             for_all_directions(dir) {
  1001.             if (point_in_dir(x, y, dir, &x1, &y1)) {
  1002.                 init_view_cell(x1, y1);
  1003.             }
  1004.             }
  1005.         }
  1006.         }
  1007.     }
  1008.     if (side->finalradius > 0) {
  1009.         /* (should also view terrain adj to each of these cells, since the
  1010.            viewing represents exploration) */
  1011.         apply_to_ring(side->startx, side->starty,
  1012.               1, side->finalradius - 1,
  1013.               init_view_cell);
  1014.         apply_to_ring(side->startx, side->starty,
  1015.               side->finalradius - 2, side->finalradius + 2,
  1016.               maybe_init_view_cell);
  1017.     }
  1018.     announce_progress((100 * i++) / numsides);
  1019.     }
  1020.     finish_lengthy_process();
  1021. }
  1022.  
  1023. /* Method to give all units and terrain a basic stockpile of supply. */
  1024.  
  1025. int
  1026. make_initial_materials()
  1027. {
  1028.     int m, t, amts[MAXTTYPES], doany, x, y;
  1029.     Unit *unit;
  1030.  
  1031.     /* This should not be run if any unit supplies were read in */
  1032.     for_all_units(unit) {
  1033.     init_supply(unit);
  1034.     }
  1035.     /* Go over each material and terrain type, looking for nonzero
  1036.        material in terrain possibilities, then alloc and fill in layers
  1037.        as needed. */
  1038.     for_all_material_types(m) {
  1039.     doany = FALSE;
  1040.     for_all_terrain_types(t) {
  1041.         amts[t] = min(tm_storage_x(t, m), tm_initial(t, m));
  1042.         if (amts[t] > 0) doany = TRUE;
  1043.     }
  1044.     if (doany) {
  1045.         allocate_area_material(m);
  1046.         for_all_cells(x, y) {
  1047.         t = terrain_at(x, y);
  1048.         set_material_at(x, y, m, amts[t]);
  1049.         }
  1050.     }
  1051.     }
  1052.     return TRUE;
  1053. }
  1054.  
  1055. /* Give the unit what it is declared to have stockpiled
  1056.    at the start of a game. */
  1057.  
  1058. void
  1059. init_supply(unit)
  1060. Unit *unit;
  1061. {
  1062.     int m, u = unit->type;
  1063.  
  1064.     for_all_material_types(m) {
  1065.     /* (should try to fill non-specific storage also somehow) */
  1066.     unit->supply[m] = min(um_storage_x(u, m), um_initial(u, m));
  1067.     }
  1068. }
  1069.  
  1070. /* Quicky test needed in a couple places. */
  1071.  
  1072. int
  1073. saved_game()
  1074. {
  1075.     FILE *fp;
  1076.  
  1077.     fp = fopen(saved_game_filename(), "r");
  1078.     if (fp != NULL) {
  1079.     fclose(fp);
  1080.     return TRUE;
  1081.     } else {
  1082.     return FALSE;
  1083.     }
  1084. }
  1085.  
  1086. /* Count all the initial units in each side's balance sheet, but only
  1087.    if the balance sheet is blank. */
  1088.  
  1089. /* (this is like restoring a history log) */
  1090.  
  1091. /* (if no log was found, we're starting over - else re-create log and caches)*/
  1092.  
  1093. void
  1094. init_side_balance()
  1095. {
  1096. #if 0
  1097.     int u, num = 0;
  1098.     Unit *unit;
  1099.     Side *side;
  1100.  
  1101.     /* Should decide whether the balance has already been set from a file. */
  1102.     for_all_sides(side) {
  1103. /*    for_all_unit_types(u) num += side_balance(side, u, FIRSTUNIT); */
  1104.     if (num == 0) {
  1105.         for_all_side_units(side, unit) {
  1106. /*        side_balance(side, unit->type, FIRSTUNIT)++;  */
  1107.         /* record_event(H_UNIT_STARTED_WITH); */
  1108.         }
  1109.     }
  1110.     }
  1111. #endif
  1112. }
  1113.  
  1114. /* This routine does a set of checks to make sure that Xconq's data
  1115.    is in a valid state.  This is particularly important after init,
  1116.    since the combination of files and synthesis methods may have
  1117.    caused some sort of disaster. */
  1118.  
  1119. void
  1120. check_consistency()
  1121. {
  1122.     int x, y;
  1123.  
  1124.     /* If no terrain, make a flat area of all ttype 0. */
  1125.     if (!terrain_defined()) {
  1126.     init_warning("No terrain defined, substituting type 0");
  1127.     allocate_area_terrain();
  1128.     for_all_cells(x, y) {
  1129.         if (inside_area(x, y)) {
  1130.         set_terrain_at(x, y, 0);
  1131.         }
  1132.     }
  1133.     add_edge_terrain();
  1134.     }
  1135.     if (numsides <= 0) {
  1136.     init_error("There are no player sides at all in this game");
  1137.     } else if (numsides < numplayers) {
  1138.     init_warning("Only made %d of the %d sides requested",
  1139.              numsides, numplayers);
  1140.     }
  1141.     /* Need any kind of unit checks? */
  1142. }
  1143.  
  1144. /* This does the actual assignment of players to sides, and initializes the
  1145.    side structures appropriately. */
  1146.  
  1147. void
  1148. assign_players_to_sides()
  1149. {
  1150.     int i, numdisplays = 0, numdisplayswanted = 0, numais = 0, n = 0;
  1151.     Side *side;
  1152.     Player *player;
  1153.  
  1154.     announce_lengthy_process("Assigning players to sides");
  1155.     for (i = 0; i < numsides; ++i) {
  1156.     announce_progress((100 * i) / numsides);
  1157.     side = assignments[i].side;
  1158.     player = assignments[i].player;
  1159.     canonicalize_player(player);
  1160.     /* Fix any mistaken advantages. */
  1161.     /* This is a warning here because properly-done interfaces shouldn't
  1162.        allow any mistaken advantages to get this far. */
  1163.     if (player->advantage < side->minadvantage) {
  1164.         init_warning("Requested advantage of %d for %s is too low, will be set to %d",
  1165.              player->advantage, player_desig(player), side->minadvantage);
  1166.         player->advantage = side->minadvantage;
  1167.     }
  1168.     if (player->advantage > side->maxadvantage) {
  1169.         init_warning("Requested advantage of %d for %s is too high, will be set to %d",
  1170.              player->advantage, player_desig(player), side->maxadvantage);
  1171.         player->advantage = side->maxadvantage;
  1172.     }
  1173.     /* Call the interface code to initialize the side's display, if
  1174.        it wants to use one (the interface has to decide). */
  1175.     if (side_wants_display(side)) {
  1176.         ++numdisplayswanted;
  1177.         init_ui(side);
  1178.         if (side_has_display(side)) {
  1179.         ++numdisplays;
  1180.         }
  1181.     }
  1182.     /* Count the desired AIs, for setup below. */
  1183.     if (side_wants_ai(side)) {
  1184.         ++numais;
  1185.     }
  1186.     Dprintf("Assigned %s to %s\n",
  1187.          side_desig(side), player_desig(player));
  1188.     }
  1189.     finish_lengthy_process();
  1190.     if (numdisplays < numdisplayswanted) {
  1191.     if (numdisplays < 1) {
  1192.         init_warning("None of the %d requested displays opened",
  1193.              numdisplayswanted);
  1194.     } else {
  1195.         init_warning("Only %d of %d requested displays opened",
  1196.              numdisplays, numdisplayswanted);
  1197.     }
  1198.     } else if (numdisplays == 0) {
  1199.     init_warning("Need at least one display to run");
  1200.     }
  1201. #ifdef DESIGNERS
  1202.     /* Make each displayed side into a designer if it was requested. */
  1203.     if (allbedesigners) {
  1204.         for_all_sides(side) {
  1205.         if (side_has_display(side)) {
  1206.         become_designer(side);
  1207.         }
  1208.         }
  1209.     }
  1210. #endif /* DESIGNERS */
  1211.     if (numais > 0) {
  1212.     announce_lengthy_process("Setting up AIs");
  1213.     for (i = 0; i < numsides; ++i) {
  1214.         if (numais > 1) announce_progress((100 * n++) / numais);
  1215.         side = assignments[i].side;
  1216.         if (side_wants_ai(side)) {
  1217.         init_ai(side);
  1218.         }
  1219.     }
  1220.     finish_lengthy_process();
  1221.     }
  1222. }
  1223.  
  1224. /* Return the program version. */
  1225.  
  1226. char *
  1227. version_string()
  1228. {
  1229.     return VERSION;
  1230. }
  1231.  
  1232. /* Return the copyright notice. */
  1233.  
  1234. char *
  1235. copyright_string()
  1236. {
  1237.     return COPYRIGHT;
  1238. }
  1239.  
  1240. /* Return the license string. */
  1241.  
  1242. char *
  1243. license_string()
  1244. {
  1245.     return "\
  1246. Xconq is free software and you are welcome to distribute copies of it\n\
  1247. under certain conditions; type \"o copying\" to see the conditions.\n\
  1248. There is absolutely no warranty for Xconq; type \"o warranty\" for details.\n\
  1249. ";
  1250. }
  1251.  
  1252. /* This comment is a fake reference to K_NO_X, which is a keyword used
  1253.    to clear the subtype-x property of terrain, but is not actually
  1254.    mentioned in the code - besides here anyway. :-) */
  1255.